package com.kc.web.controller.business;

import com.google.common.collect.Lists;
import com.kc.business.domain.KcFreeTicketApply;
import com.kc.business.domain.vo.KcFreeTicketApplyVo;
import com.kc.business.enums.FreeTicketApplyStatusEnum;
import com.kc.business.service.IBizFreeTicketService;
import com.kc.common.core.controller.BaseController;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.core.domain.entity.SysDictData;
import com.kc.common.core.page.TableDataInfo;
import com.kc.system.service.ISysDictDataService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * FileName: BizFreeTicketController
 * Author:   TP
 * Date:     2020-10-28 13:49
 * Description:
 */
@RestController
@RequestMapping("/business/freeTicket")
public class BizFreeTicketController extends BaseController {

    @Autowired
    private IBizFreeTicketService bizFreeTicketService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    @PreAuthorize("@ss.hasPermi('business:freeTicket:list')")
    @GetMapping("/list")
    public TableDataInfo list(KcFreeTicketApplyVo vo) {
        startPage();
        List<KcFreeTicketApply> applies = bizFreeTicketService.getList(vo);
        // 其实这里本应该适合前台来弄
        List<KcFreeTicketApplyVo> result = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(applies)) {
            SysDictData sysDictData = new SysDictData();
            sysDictData.setDictType("ticket_free_people_type");
            List<SysDictData> dictData = sysDictDataService.selectDictDataList(sysDictData);
            for (KcFreeTicketApply apply : applies) {
                KcFreeTicketApplyVo applyVo = new KcFreeTicketApplyVo();
                BeanUtils.copyProperties(apply, applyVo);
                applyVo.setRealname(apply.getRealname());
                applyVo.setMobile(apply.getMobile());
                applyVo.setCertificatesImgUrls(apply.getCertificatesImgUrl().split(","));
                for (SysDictData data : dictData) {
                    if (data.getDictValue().equals(apply.getFreePeopleType())) {
                        applyVo.setFreePeopleTypeDesc(data.getDictLabel());
                        break;
                    }
                }
                result.add(applyVo);
            }
        }
        // 这里需要重新设置转换后的rows，直接使用getDataTable(result)会造成total错误
        TableDataInfo tableDataInfo = getDataTable(applies);
        tableDataInfo.setRows(result);
        return tableDataInfo;
    }

    @PreAuthorize("@ss.hasPermi('business:freeTicket:audit')")
    @PutMapping("/auditBatch")
    public AjaxResult auditBatch(String ids, boolean auditPass, String rejectReason) {
        if (StringUtils.isNotEmpty(ids)) {
            List<Integer> toAuditIds = Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList());
            if (auditPass) {
                return bizFreeTicketService.auditBatch(toAuditIds, FreeTicketApplyStatusEnum.AUDIT_PASS.getStatus(),
                        IBizFreeTicketService.AUDIT_PASS_MESSAGE);
            }
            return bizFreeTicketService.auditBatch(toAuditIds, FreeTicketApplyStatusEnum.AUDIT_REJECT.getStatus(), rejectReason);
        }
        return AjaxResult.error("请选择要审批的销售记录");
    }

    @PreAuthorize("@ss.hasPermi('business:freeTicket:audit')")
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody KcFreeTicketApplyVo vo) {
        if (vo.getId() > 0) {
            if (vo.getAuditPass() == 1) {
                return bizFreeTicketService.audit(vo.getId(), FreeTicketApplyStatusEnum.AUDIT_PASS.getStatus(),
                        IBizFreeTicketService.AUDIT_PASS_MESSAGE);
            } else if (vo.getAuditPass() == 0) {
                return bizFreeTicketService.audit(vo.getId(), FreeTicketApplyStatusEnum.AUDIT_REJECT.getStatus(),
                        vo.getRejectReason());
            } else {
                return AjaxResult.error("请指定审批结果");
            }
        }
        return AjaxResult.error("请选择要审批的销售记录");
    }

}
